<script type="text/javascript">
/* eslint-disable */
import wx from 'weixin-js-sdk';
  var openid = "";

  /**
   * 获取openid
   */
  function getOpenid() {
    if (isUndefined(this.openid)) {
      return this.openid;
    } else {
      /*从链接中获取openid*/
      let request = {
        QueryString: function (val) {
          var uri = window.location.search;
          console.log(uri)
          var re = new RegExp("" + val + "=([^\&\?]*)", "ig");
          return ((uri.match(re)) ? (uri.match(re)[0].substr(val.length + 1)) : null);
        }
      };

      this.openid = request.QueryString("openid");
      let openidlo = localStorage.getItem("openid");

      this.openid = this.openid == null ? openidlo : this.openid;
      console.log(this.openid)
      if (this.openid == null && isWinxin()) { //如果本地或者url无openid，则微信获取
        let href = getRootPath_dc();

        let url = encodeURIComponent("http://www.zhenxc.com/feesys/generalize/share?url=" + href);
        console.log(url);
        //直接调转到页面上。
        location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx13ce2540d20504d5&redirect_uri=" + url + "&response_type=code&scope=snsapi_base#wechat_redirect";
      } else {
        localStorage.setItem("openid", this.openid);
      }
      return this.openid;
    }
  }

  /**
   * 判断是否为微信浏览器
   */
  function isWinxin() {
    const ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
      return true;
    } else {
      return false;
    }
  };

  /**
   *
   *
   **/
  function getRootPath_dc() {
    var pathName = window.location.pathname.substring(1);
    var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
    if (webName == "") {
      return window.location.protocol + '//' + window.location.host;
    } else {
      return window.location.protocol + '//' + window.location.host + '/' + webName;
    }
  };

  function isUndefined(data) {
		if(data != undefined && data.length > 0) {
			return true;
		} else {
			return false;
		}
	}

  function getRequestURL() {
		var requestURL1 = {
			QueryString: function(val) {
				var uri = window.location.search;
				var re = new RegExp("" + val + "=([^\&\?]*)", "ig");
				return((uri.match(re)) ? (uri.match(re)[0].substr(val.length + 1)) : null);
			}
		};
		return requestURL1;
	}

	/**
	 * 初始化分享
	 * @param {Object} data
	 */
	function initwxconfig(data) {

		if(data != undefined) {
			this.timestamp = data.timestamp;
			this.appId = data.appId;
			this.nonceStr = data.nonceStr;
			this.signature = data.signature;

			localStorage.setItem('timestamp', this.timestamp);
			localStorage.setItem('appId', this.appId);
			localStorage.setItem('nonceStr', this.nonceStr);
			localStorage.setItem('signature', this.signature);
		} else {
			this.timestamp = localStorage.getItem('timestamp');
			this.appId = localStorage.getItem('appId');
			this.nonceStr = localStorage.getItem('nonceStr');
			this.signature = localStorage.getItem('signature');
		}

		wx.config({
			debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
			appId: this.appId, // 必填，公众号的唯一标识
			timestamp: this.timestamp, // 必填，生成签名的时间戳
			nonceStr: this.nonceStr, // 必填，生成签名的随机串
			signature: this.signature, // 必填，签名，见附录1
			//jsApiList: ["onMenuShareQZone", "onMenuShareQQ", "onMenuShareTimeline", "onMenuShareAppMessage"] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
			jsApiList: ["onMenuShareQZone", "onMenuShareQQ", "onMenuShareTimeline", "onMenuShareAppMessage"] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
		});
		//处理验证失败的信息
		wx.error(function(res) {
			console.log(res);
		});
	}
  export default {
    getOpenid,
    isWinxin,
    getRootPath_dc,
    isUndefined,
    getRequestURL,
    initwxconfig
  }
</script>
